<template>
  <div class="app-container">
    <ele-form
      v-model="formData"
      v-bind="formConfig"
      :request-fn="handleRequest"
      :is-show-back-btn="false"
      @request-success="handleRequestSuccess"
    />
  </div>
</template>

<script>
import {
  getcode,
  setpassword
} from '@projectName/api/profile/profile.js'
export default {
  props: {
    userinfo: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      formData: {
        code: '',
        mobile: '',
        password_reset_token: ''
      },
      formConfig: {
        formSpan: 24,
        labelPosition: 'top',
        formDesc: {
          mobile: {
            type: 'input',
            label: '手机号',
            attrs: {
              disabled: true
            }
          },
          newpassword: {
            type: 'input',
            label: '密码'
          },
          code: {
            type: 'input',
            label: '验证码',
            slots: {
              'append': (h) => {
                return h('span', { class: 'text-pointer', on: {
                  click: () => {
                    this.getcode()
                  }
                }}, '发送验证码')
              }
            }
          }
        },
        order: ['mobile', 'newpassword']
      }
    }
  },
  watch: {
    userinfo(val) {
    }
  },
  created() {
    const that = this
    that.formData.mobile = that.userinfo.mobile
    that.formData.password_reset_token = that.userinfo.password_reset_token
  },
  methods: {
    // 获取验证码
    getcode() {
      const that = this
      if (!that.formData.mobile) {
        that.$message({
          message: '请输入手机号码',
          type: 'warning'
        })
        return false
      }
      const data = {
        mobile: that.formData.mobile,
        type: 'forgetpass'
      }
      getcode(data).then((res) => {
        that.$message({
          message: res.message,
          type: 'success'
        })
      })
    },
    handleRequest(data) {
      const that = this
      console.log(that.userinfo)
      setpassword(data).then((res) => {
        that.$message({
          message: res.message,
          type: 'success'
        })
      })
    },
    handleRequestSuccess() {}
  }
}
</script>
